package org.bing.engine.controller.service;

import java.util.Date;

import org.bing.engine.controller.preference.PreferenceManager;
import org.bing.engine.core.domain.Controller;
import org.bing.engine.utility.helper.HostHelper;
import org.bing.engine.utility.helper.NetworkHelper;

public class LocalControllerManager {
    private static final Date START_TIME = new Date();
    protected PreferenceManager preference;
    private int controllerPort;
    private String controllerVersion;
    private Controller controller;

    public void setControllerPort(int port) {
        this.controllerPort = port;
    }

    public void setControllerVersion(String version) {
        this.controllerVersion = version;
    }

    public void setPreferenceManager(PreferenceManager fm) {
        this.preference = fm;
    }

    public Controller localController() {
        if (controller == null) {
            controller = new Controller();
            controller.setAddress(HostHelper.getHostAddress());
            controller.setHostname(HostHelper.getHostName());
            controller.setGateway(NetworkHelper.getGateway());
            controller.setGlobalId(preference.getControllerGuid());
            controller.setStartTime(START_TIME);
            controller.setPort(controllerPort);
            controller.setVersion(controllerVersion);
        }
        controller.setLastActivityTime(new Date());
        return controller;
    }
}
